package com.cskaoyan.demo6.service;

import com.cskaoyan.demo6.bean.model.MarketCoupon;
import com.cskaoyan.demo6.bean.model.MarketCouponExample;
import com.cskaoyan.demo6.bean.vo.HomeIndexVo;
import com.cskaoyan.demo6.mapper.MarketCouponMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * @Description：
 * @Author：BLUEBLANK
 * @Date：2024/4/21 16:25
 */

@Service("couponService")
public class CouponServiceImpl implements CouponService {
    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Override
    public List<HomeIndexVo.CouponListDTO> select(Integer offset, Integer couponLimit) {
        PageHelper.offsetPage(offset, couponLimit);
        MarketCouponExample couponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = couponExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        couponExample.setOrderByClause("add_time desc");
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(couponExample);
        List<HomeIndexVo.CouponListDTO> couponList = new ArrayList<>();

        marketCoupons.forEach(coupon -> {
            HomeIndexVo.CouponListDTO couponListDTO = new HomeIndexVo.CouponListDTO();
            BeanUtils.copyProperties(coupon, couponListDTO);
            couponList.add(couponListDTO);
        });

        return couponList;
    }
}
